﻿(function($) {
    function Settings() {
        this.peramount = 10;
        this.address = "GetMoreComments";
        this.totalCountAddress = "GetCommentCount";
        this.objectID = "";
        this.amount = 20;
        this.width = 500;
        this.commentType;
        this.parent = null;
        this.elementAppend = false;
        this.autoload = true;
    }
    var methods = {
        init: function(params) {
            var self = this;
            var settings = new Settings();
            settings.parent = self;
            if (params) {
                $.extend(settings, params);
            }
            self.data("settings", settings);
            self.find('.more').more(settings);
            self.find('.btnCommit').bind('click.comment', function() {
                $(this).hide();
                $(this).before($("<img class='loading' alt='' src='/images/loading.gif'/>"));
                self.comment("commit");
            });
            self.css("width", settings.width);
            var txtComment = self.find(".txtComment");
            txtComment.autoResize();
        },
        reset: function(params) {
            var settings = this.data("settings");
            if (params) {
                $.extend(settings, params);
            }
            this.find('.more').more("reset", settings);
        },
        commit: function() {
            var self = this;
            var settings = self.data("settings");
            var txtComment = self.find(".txtComment");
            if (txtComment.val().length == 0) {
                alert("请先输入评论!");
                var loading = self.find('.btnCommit').prev();
                if (loading.length > 0 && loading[0].className == "loading") {
                    loading.remove();
                }
                self.find('.btnCommit').show();

                return;
            }

            var replyName = self.find(".replyName");
            var replyTo = replyName.find(".AuthorName");
            var replyToID = "";
            if (replyTo.length > 0) {
                replyToID = replyTo.data("replyToID");
            }

            var params = {
                subjectID: settings.objectID,
                text: txtComment.val(),
                replyTo: replyToID,
                commentType: settings.commentType
            };
            $.CallSelfAjaxMethod("SaveComment", params, function(data) {
                if (data.d) {
                    replyName.empty();
                    txtComment.val("");
                    var more = self.find(".more");
                    var comments = [];
                    comments.push(data.d);
                    more.more("append_elements", comments);

                    var loading = self.find('.btnCommit').prev();
                    if (loading.length > 0 && loading[0].className == "loading") {
                        loading.remove();
                    }
                    self.find('.btnCommit').show();
                    txtComment.height(30);
                }


            });
        }

    };

    $.fn.comment = function(method) {
        if (methods[method])
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        else if (typeof method == 'object' || !method)
            return methods.init.apply(this, arguments);
        else $.error('Method ' + method + ' does not exist!');
    }
})(jQuery);